<?php

require_once("global.php");
require_once("EKEvaluation.php");

class EKUser
{
	private $id;
	private $password;
	private $name;
	private $type;
	private $email;
	private $selfIntro;
	private $exp;
	private $ratingA;
	private $ratingAWeight;
	private $ratingB;
	private $ratingBWeight;
	private $finalExp;
	private $level;
	private $enabled;
	
	private $documentCreatedIds;
	private $paragraphCreatedIds;
	private $talkCreatedIds;
	private $ratingCreatedIds;
	
	function __construct($id, $getDisabled = false)
	{
		db_connect();
		$sql = "select * from ekuser where id = '".$id."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		
		if($row && ($row["enabled"]||$getDisabled))
		{
			$this->id = $id;
			$this->password = $row["password"];
			$this->name = $row["name"];
			$this->type = $row["type"];
			$this->email = $row["email"];
			$this->selfIntro = $row["selfIntro"];
			$this->exp = $row["exp"];
			$this->enabled = $row["enabled"];
			$this->ratingA = $row["ratingA"];
			$this->ratingAWeight = $row["ratingAWeight"];
			$this->ratingB = $row["ratingB"];
			$this->ratingBWeight = $row["ratingBWeight"];
			$this->documentCreatedIds = array();
			$this->paragraphCreatedIds = array();
			$this->ratingCreatedIds = array();
			$this->talkCreatedIds = array();
			$this->finalExp = EKEvaluation::transformExp($this->exp,
						$this->ratingA,$this->ratingAWeight,
						$this->ratingB,$this->ratingBWeight);
			$this->level = EKEvaluation::calculateLv($this->finalExp);
		}else{
			mysql_close();
			throw new Exception("No such user: ".$id."!");
			return;
		}
		
		$sql = "select * from ekuser_d where userId = '".$id."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$this->documentCreatedIds[] = "_".$row["id"];
			$row = mysql_fetch_array($result);
		}
		
		$sql = "select * from ekuser_p where userId = '".$id."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$this->paragraphCreatedIds[] = "_".$row["id"];
			$row = mysql_fetch_array($result);
		}
		
		$sql = "select * from ekuser_r where userId = '".$id."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$this->ratingCreatedIds[] = "_".$row["id"];
			$row = mysql_fetch_array($result);
		}
		
		$sql = "select * from ekuser_t where userId = '".$id."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$this->talkCreatedIds[] = "_".$row["id"];
			$row = mysql_fetch_array($result);
		}
		mysql_close();
	}
	
	static function register($id, $password, $name, $email, $selfIntro)
	{
		db_connect();
		$sql = "select * from ekuser where id = '".$id."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		if($row)
		{
			mysql_close();
			throw new Exception("The user id '".$id."' already exists.");
			return;
		}
		
		$sql = "insert into ekuser (id, password, name, email, selfIntro, enabled) values 
			('".$id."', '"
		       .$password."', '"
		       .$name."', '"
		       .$email."', '"
		       .$selfIntro."', '0')";
	    $result = mysql_query($sql);

	    if (!$result){
	        throw new Exception("MySQL error: ".mysql_error());
	    }
		
		mysql_close();
	}
	
	# Insert a record into DB
	static function insertRecord($userId,$type,$elementId){
		db_connect();
		$sql = "select * from ekuser_".$type." where userId = '"
			.$userId."' and id = '".substr($elementId,1)."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		if(!$row)
		{
			$sql = "insert into ekuser_".$type." (userId, id) values 
				('".$userId."', '"
		       	.substr($elementId,1)."')";
			$result = mysql_query($sql);
		    if (!$result){
		        throw new Exception("MySQL error: ".mysql_error());
		    }
		}
		mysql_close();
	}
	
	static function delRecord($userId,$type,$elementId){
		db_connect();
		$sql = "delete from ekuser_".$type." where userId = '".$userId."' and id = '".substr($elementId,1)."'";
		$result = mysql_query($sql);
	    if (!$result){
	        throw new Exception("MySQL error: ".mysql_error());
	    }
		mysql_close();
	}
	
	static function updateDB($userId, $Attribute, $newValue)
	{
		db_connect();
		$sql = "update ekuser set ".$Attribute." = '".$newValue."' where id = '".$userId."'";
		$result = mysql_query($sql);
		if(!$result)
		{
			mysql_close();
			throw new Exception("Update fails.");
		}
		mysql_close();
	}
	
	function getEnabled(){
		return $this->enabled;
	}
	
	function getId(){return $this->id;}
	function getName(){return $this->name;}
	function getType(){return $this->type;}
	function getEmail(){return $this->email;}
	function getSelfIntro(){return $this->selfIntro;}
	function getExp(){return floor($this->finalExp);}
	function getLevel(){return $this->level;}
	
	function getRatingA(){return $this->ratingA;}
	function getRatingB(){return $this->ratingB;}
	function getRatingAWeight(){return $this->ratingAWeight;}
	function getRatingBWeight(){return $this->ratingBWeight;}
	function getDocumentCreatedIds() {
		return $this->documentCreatedIds;
	}
	function getParagraphCreatedIds() {
		return $this->paragraphCreatedIds;
	}
	function getTalkCreatedIds() {
		return $this->talkCreatedIds;
	}
	
	static function validate($userId, $password)
	{
		db_connect();
		$sql = "select * from ekuser where id = '".$userId.
			"' and password = '".$password.
		    "' and enabled = 1";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		mysql_close();
		if($row)
		{
			return true;
		}else{
			return false;
		}
	}
	
	static function getAttributeById($userId, $Attribute)
	{
		db_connect();
		$sql = "select * from ekuser where id = '".$userId."'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		mysql_close();
		return $row[$Attribute];
	}
	
	static function getAllNormalUsersSortedByExp($getDisabled)
	{
		db_connect();
		if($getDisabled)
		{
			$sql = "select id from ekuser where type <= 1";
		}else{
			$sql = "select id from ekuser where type <= 1 and enabled <> 0";
		}
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		while($row)
		{
			$ids[] = $row["id"];
			$row = mysql_fetch_array($result);
		}
		mysql_close();
		if(count($ids)>0){
			foreach($ids as $id){
				$user[] = new EKUser($id, $getDisabled);
			}
		}
		return EKUser::orderUserByExp($user);
	}
	
	private static function findMaxExp($list, $from)
	{
		$max = 0;
		$r = $from;
		for($i = $from; $i<count($list); ++$i)
		{
			if($list[$i]->finalExp > $max)
			{
				$max = $list[$i]->finalExp;
				$r = $i;
			}
		}
		return $r;
	}
	

	private static function orderUserByExp($list)
	{
		if(count($list) != 0){
			for($i=0; $i<count($list); ++$i)
			{
				$maxIndex = EKUser::findMaxExp($list, $i);
				$r[] = $list[$maxIndex];
				$list[$maxIndex] = $list[$i];
			}
			return $r;
		} else return 0;
	}
	
	
	
}
?>